import 'package:hive/hive.dart';

part 'playlist.g.dart';

/// 播放列表数据模型类
/// 包含播放列表的基本信息和音乐列表
@HiveType(typeId: 1)
class Playlist {
  /// 播放列表唯一标识符
  @HiveField(0)
  final String id;

  /// 播放列表名称
  @HiveField(1)
  final String name;

  /// 播放列表描述
  @HiveField(2)
  final String? description;

  /// 播放列表封面图片路径
  @HiveField(3)
  final String? coverImage;

  /// 创建时间
  @HiveField(4)
  final DateTime createdAt;

  /// 更新时间
  @HiveField(5)
  final DateTime updatedAt;

  /// 播放列表中的音乐ID列表
  @HiveField(6)
  final List<String> musicIds;

  /// 是否为系统默认播放列表
  @HiveField(7)
  final bool isDefault;

  /// 构造函数
  Playlist({
    required this.id,
    required this.name,
    this.description,
    this.coverImage,
    required this.createdAt,
    required this.updatedAt,
    required this.musicIds,
    this.isDefault = false,
  });

  /// 从JSON创建Playlist对象
  factory Playlist.fromJson(Map<String, dynamic> json) {
    return Playlist(
      id: json['id'] as String,
      name: json['name'] as String,
      description: json['description'] as String?,
      coverImage: json['cover_image'] as String?,
      createdAt: DateTime.fromMillisecondsSinceEpoch(json['created_at'] as int),
      updatedAt: DateTime.fromMillisecondsSinceEpoch(json['updated_at'] as int),
      musicIds: json['music_ids'] is String 
          ? (json['music_ids'] as String).split(',')
          : List<String>.from(json['music_ids'] as List), // 处理字符串和List两种格式
      isDefault: json['is_default'] is int 
          ? (json['is_default'] as int) == 1 
          : json['is_default'] as bool? ?? false, // 处理int和bool两种格式
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'description': description,
      'cover_image': coverImage,
      'created_at': createdAt.millisecondsSinceEpoch,
      'updated_at': updatedAt.millisecondsSinceEpoch,
      'music_ids': musicIds.join(','), // 将List<String>转换为字符串
      'is_default': isDefault ? 1 : 0, // 将bool转换为int
    };
  }

  /// 复制并修改部分属性
  Playlist copyWith({
    String? id,
    String? name,
    String? description,
    String? coverImage,
    DateTime? createdAt,
    DateTime? updatedAt,
    List<String>? musicIds,
    bool? isDefault,
  }) {
    return Playlist(
      id: id ?? this.id,
      name: name ?? this.name,
      description: description ?? this.description,
      coverImage: coverImage ?? this.coverImage,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      musicIds: musicIds ?? List<String>.from(this.musicIds),
      isDefault: isDefault ?? this.isDefault,
    );
  }

  /// 添加音乐到播放列表
  Playlist addMusic(String musicId) {
    if (musicIds.contains(musicId)) return this;
    final newMusicIds = List<String>.from(musicIds)..add(musicId);
    return copyWith(
      musicIds: newMusicIds,
      updatedAt: DateTime.now(),
    );
  }

  /// 从播放列表移除音乐
  Playlist removeMusic(String musicId) {
    final newMusicIds = List<String>.from(musicIds)..remove(musicId);
    return copyWith(
      musicIds: newMusicIds,
      updatedAt: DateTime.now(),
    );
  }

  /// 重新排序播放列表中的音乐
  Playlist reorderMusic(int oldIndex, int newIndex) {
    final newMusicIds = List<String>.from(musicIds);
    final item = newMusicIds.removeAt(oldIndex);
    newMusicIds.insert(newIndex, item);
    return copyWith(
      musicIds: newMusicIds,
      updatedAt: DateTime.now(),
    );
  }

  /// 获取播放列表中音乐的数量
  int get musicCount => musicIds.length;

  /// 检查播放列表是否为空
  bool get isEmpty => musicIds.isEmpty;

  /// 检查播放列表是否包含指定音乐
  bool containsMusic(String musicId) => musicIds.contains(musicId);

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is Playlist && other.id == id;
  }

  @override
  int get hashCode => id.hashCode;

  @override
  String toString() {
    return 'Playlist(id: $id, name: $name, musicCount: $musicCount)';
  }
}

/// 播放列表项数据模型
/// 用于表示播放列表中的单个音乐项
@HiveType(typeId: 2)
class PlaylistItem {
  /// 播放列表项唯一标识符
  @HiveField(0)
  final String id;

  /// 播放列表ID
  @HiveField(1)
  final String playlistId;

  /// 音乐ID
  @HiveField(2)
  final String musicId;

  /// 排序顺序
  @HiveField(3)
  final int sortOrder;

  /// 添加时间
  @HiveField(4)
  final DateTime addedAt;

  /// 构造函数
  PlaylistItem({
    required this.id,
    required this.playlistId,
    required this.musicId,
    required this.sortOrder,
    required this.addedAt,
  });

  /// 从JSON创建PlaylistItem对象
  factory PlaylistItem.fromJson(Map<String, dynamic> json) {
    return PlaylistItem(
      id: json['id'] as String,
      playlistId: json['playlist_id'] as String,
      musicId: json['music_id'] as String,
      sortOrder: json['sort_order'] as int,
      addedAt: DateTime.fromMillisecondsSinceEpoch(json['added_at'] as int),
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'playlist_id': playlistId,
      'music_id': musicId,
      'sort_order': sortOrder,
      'added_at': addedAt.millisecondsSinceEpoch,
    };
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is PlaylistItem && other.id == id;
  }

  @override
  int get hashCode => id.hashCode;

  @override
  String toString() {
    return 'PlaylistItem(id: $id, playlistId: $playlistId, musicId: $musicId)';
  }
}
